const axios = require('axios')
const BASE_URL = 'https://gitee.com/api/v5'

class GiteeRequest {
  constructor(token) {
    this.token = token
    this.service = axios.create({
      baseURL: BASE_URL,
      timeout: 5000,
    })
    this.service.interceptors.response.use(
      res => {
        return res.data
      },
      err => {
        if (err.response && err.response.data) {
          return err.response
        } else {
          return Promise.reject(err)
        }
      }
    )
  }

  get(url, params, headers) {
    return this.service({
      url,
      params: {
        ...params,
        access_token: this.token,
      },
      method: 'get',
      headers,
    })
  }

  post(url, data, headers) {
    return this.service({
      url,
      params: {
        access_token: this.token,
      },
      data,
      method: 'post',
      headers,
    })
  }
}

module.exports = GiteeRequest
